package proteus.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

import proteus.excepcion.DAOExcepcion;
import proteus.modelo.Persona;
import proteus.negocio.PersonaNegocio;
import proteus.util.Funciones;

/**
 * Servlet implementation class GuardarUsuarioServlet
 */
@WebServlet("/GuardarUsuarioServlet")
public class GuardarUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GuardarUsuarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		
		
		try
		{
			
			String nom=request.getParameter("txtNom");
			String pat=request.getParameter("txtApePaterno");
			String mat=request.getParameter("txtApeMaterno");
			String tipPersona=request.getParameter("cboTipoPersona");
			String email=request.getParameter("txtCorreo");
			
			
			String sex=request.getParameter("cboSexo");
			String tip=request.getParameter("cboTipo");
			String nro=request.getParameter("txtNroDocumento");
			String pwd=request.getParameter("txtClave01");
			
			String txtFecNac=request.getParameter("txtFecNac");
			
			if(txtFecNac.length()==0){
				throw new DAOExcepcion("La Fecha es un campo obligatorio");	
			}	
			txtFecNac=Funciones.cambiaFormatoFecha(txtFecNac);
			java.sql.Date javaSqlDate = java.sql.Date.valueOf(txtFecNac);
			//String fec="1989-06-06";
			//java.sql.Date javaSqlDate = java.sql.Date.valueOf(fec);
			
			
			//String fec=request.getParameter("txtFecNac");
			String cel=request.getParameter("txtNroCel");
			
			Persona per=new Persona();
			per.setNoPersona(nom);
			per.setApPaterno(pat);
			per.setApMaterno(mat);
			per.setCoTipoPersona(tipPersona);
			per.setTxCorreo(email);
			per.setFlSexo(sex);
			per.setCoTipoDocumento(tip);
			per.setNuDocumento(nro);
			per.setTxClave(pwd);
			per.setFeNacimiento(javaSqlDate);
			per.setNuCelular(cel);
			
			PersonaNegocio neg=new PersonaNegocio();
			neg.insertarPersona(per);
//			response.sendRedirect("Confirma")
			request.setAttribute("msgerror", "Se registro correctamente");
		}
		catch(DAOExcepcion e){
			request.setAttribute("msgerror", e.getMessage());		
			
			
		}
		
		
		RequestDispatcher rd=request.getRequestDispatcher("registrarUsuario.jsp");
		rd.forward(request, response);
		
		
		
		
	}

}
